package com.bestvike.portal.data;

import com.bestvike.commons.entity.BaseData;
import com.bestvike.commons.utils.StringUtils;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Entity
public class SysDept extends BaseData implements Serializable {
    private  static  final long serialVersionUID = 1L;

    @Id
    private String id;
    private String name;
    private Integer grade;
    private String status;
    private String parent;
    private Integer sn;
    private String directorId;
    private String directorName;
    private String remark;

    @Transient
    private List<SysDept> children;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getGrade() {
        return grade;
    }

    public void setGrade(Integer grade) {
        this.grade = grade;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getParent() {
        return parent;
    }

    public void setParent(String parent) {
        this.parent = parent;
    }

    public Integer getSn() {
        return sn;
    }

    public void setSn(Integer sn) {
        this.sn = sn;
    }

    public String getDirectorId() {
        return directorId;
    }

    public void setDirectorId(String directorId) {
        this.directorId = directorId;
    }

    public String getDirectorName() {
        return directorName;
    }

    public void setDirectorName(String directorName) {
        this.directorName = directorName;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<SysDept> getChildren() {
        return children;
    }

    public void setChildren(List<SysDept> children) {
        this.children = children;
    }

    ////////////////////// 递归处理树 //////////////////////
    public static List<SysDept> buildTree(List<SysDept> sysDeptList) {
        List<SysDept> resultList = new ArrayList<>();
        for (SysDept sysDept : getRootNode(sysDeptList)) {
            sysDept = buildChildren(sysDeptList, sysDept);
            resultList.add(sysDept);
        }
        return resultList;
    }
    private static SysDept buildChildren(List<SysDept> sysDeptList, SysDept parentDept) {
        List<SysDept> children = new ArrayList<>();
        for (SysDept sysDept : sysDeptList) {
            if (sysDept.getParent() != null && sysDept.getParent().equals(parentDept.getId())) {
                children.add(buildChildren(sysDeptList, sysDept));
            }
        }
        parentDept.setChildren(children);
        return parentDept;
    }
    private static List<SysDept> getRootNode(List<SysDept> sysDeptList) {
        List<SysDept> rootNodeList = new ArrayList<>();
        for (SysDept sysDept : sysDeptList) {
            if (StringUtils.isEmpty(sysDept.getParent())) {
                rootNodeList.add(sysDept);
            }
        }
        return rootNodeList;
    }
}
